-- shackles
-- create by panyl
-- 枷锁

return {
    applyEffect = function(classId)
        local prop = PropM.combine(ME.user, "bonus_level", classId);
        local level = PropM.apply(prop, 0);
        if level <= 0 then
            return false;
        end

        -- 获得level * 4 件普通宝物，比例为1~6阶：5:5:4:3:3
        local count = level * 4;
        local rank;
        local rand1;
        local rand2;
        local equipId;
        local equipArr;
        local arr = { { ["rank"] = 1, ["ratio"] = 50, }, { ["rank"] = 2, ["ratio"] = 50, },
                      { ["rank"] = 3, ["ratio"] = 40, }, { ["rank"] = 4, ["ratio"] = 30, },
                      { ["rank"] = 5, ["ratio"] = 30, },  };

        local bonus;
        local bonusList = {};

        if BuffDataM.query("limited_gem", 5) == 0 then
            -- 首次保底一件钻石铠甲
            count = count - 1;
            BonusM.doBonus( {1, 8176, 1}, "shackles");
            table.insert(bonusList,{1, 8176, 1});
        end

        if level >= 4 then
            -- 4阶起，保底一件6阶装备
            rand1 = DungeonM.getRandSeed("shackles");
            equipArr = ItemM.getEquipByRank(6);
            equipId = FormulaM.invoke("FETCH_BY_RAND", equipArr, rand1);
            count = count - 1;

            bonus = {1, equipId, 1};
            BonusM.doBonus( bonus, "shackles");
            table.insert(bonusList,bonus);
        end

        if level >= 5 then
            -- 5阶起，保底1套套装，在限定套装内抽取
            rand1 = DungeonM.getRandSeed("shackles");
            local suitArr = { 1,2,6,7,11,12,13,14,15,16,17,18,19,20 };

            local suitId = FormulaM.invoke("FETCH_BY_RAND", suitArr, rand1);
            equipArr = SuitM.query(suitId, "active");

            count = count - #equipArr;
            for index = 1, #equipArr do
                bonus = {1, equipArr[index], 1};
                BonusM.doBonus( bonus, "shackles");
                table.insert(bonusList,bonus);
            end

        end

        for i = 1, count do
            rand1 = DungeonM.getRandSeed("shackles");
            rand2 = DungeonM.getRandSeed("shackles");

            rank = fetchElemBySeed(arr, rand1)["rank"];
            equipArr = ItemM.getEquipByRank(rank);
            equipId = FormulaM.invoke("FETCH_BY_RAND", equipArr, rand2)

            bonus = {1, equipId, 1};
            BonusM.doBonus( bonus, "shackles");
            table.insert(bonusList,bonus);
        end

        -- 触发特技发动事件, 先记在玩家身上
        -- ME.user.dbase:setTemp("speicial_skill_trigger", prop[1]);

        -- 奖励提示
        if #bonusList > 0 then
            local destructEquip = ME.user.destructEquip or {};
            table.insert(destructEquip, {["classId"] = classId, ["bonusList"] = bonusList, ["notifyType"] = "slime_talk", ["heroId"] = 55, });
            ME.user.destructEquip = destructEquip;
        end

        return true;
    end,

    suffixDesc = function(classId)
        local special = SelfDestructM.query(classId, "effect")["special"];

        local ret = "";
        local specialName = SkillM.query(special, "name") or "";
        for lv = 1, 5 do
            if ret ~= "" then
                ret = ret .. "\n";
            end

            local value = string.format(getLocStr("equip_random_bonus"), lv * 4);
            local levelStr = getLevelStr(lv);
            local descStr = spliceLocStr(string.format(getLocStr("brace_and_text"), spliceLocStr(specialName, levelStr)), value);
            ret = ret .. descStr;
        end

        return ret;
    end
};
